package com.demo.jsf;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.faces.context.FacesContext;

import com.demo.javabean.DBAccess;

public class ScheduleAction extends BaseAction {

	private String id;
	private String username;
	private String year;
	private String month;
	private String day;
	private String plan;

	public String list() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得分页参数
		String strPageSize = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageSize");
		if (strPageSize != null) {
			pageSize = Integer.parseInt(strPageSize);
		}
		String strPageNo = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageNo");
		if (strPageNo != null) {
			pageNo = Integer.parseInt(strPageNo);
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 查询总的记录数，计算跳页参数
			int start = pageSize * (pageNo - 1);
			// 总记录数查询SQL
			String sql1 = "select count(*) as countall from schedule where username='"
					+ loginAction.getUsername() + "'";
			db.query(sql1);
			if (db.next()) {
				// 计算总行数并保存
				String countall = db.getValue("countall");
				rowCount = Integer.parseInt(countall);

				// 计算总页数并保存
				pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
						: rowCount / pageSize + 1;

				// 计算跳页参数并保存
				pageFirstNo = 1;// 首页
				pageLastNo = pageCount;// 尾页
				pagePreNo = pageNo > 1 ? pageNo - 1 : 1;// 前一页
				pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;// 后一页
			}
			db.closeRs();

			// 取得当前页数据SQL
			String sql2 = "select * from schedule where username='" + loginAction.getUsername()
			+ "' order by year desc, month desc, day desc limit "
			+ start + "," + pageSize;
			list = new ArrayList<Hashtable<String, String>>();
			db.query(sql2);
			while (db.next()) {
				// 查询每行数据的各个字段数据
				Hashtable<String, String> hash = new Hashtable<String, String>();
				hash.put("id", db.getValue("id"));
				hash.put("username", db.getValue("username"));
				hash.put("year", db.getValue("year"));
				hash.put("month", db.getValue("month"));
				hash.put("day", db.getValue("day"));
				hash.put("plan", db.getValue("plan"));

				// 保存当前行
				list.add(hash);
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return "schedulelist";
	}

	public String add() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		return "scheduleadd";
	}

	public String insert() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 组合新增SQL
			String sql = "insert into schedule (username, year, month, day, plan) ";
			sql += " values('" + loginAction.getUsername() + "'," + year + "," + month + ","
					+ day + ",'" + plan + "')";

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行插入
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String edit() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "schedulelist";
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 根据id编号查询当前行记录
			String sql = "select * from schedule where id=" + id;
			db.query(sql);
			if (db.next()) {
				// 取得各个字段的值并保存
				username = db.getValue("username");				
				year = db.getValue("year");
				month = db.getValue("month");
				day = db.getValue("day");
				plan = db.getValue("plan");
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return "scheduleedit";
	}

	public String update() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 组合更新SQL
			String sql = "update schedule set year=" + year + ", month="
			+ month + ", day=" + day + ", plan='" + plan
			+ "' where id=" + id;

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行更新
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String delete() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "schedulelist";
		}
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 根据id组成删除SQL，执行删除
			String sql = "delete from schedule where id=" + id;
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public String getPlan() {
		return plan;
	}

	public void setPlan(String plan) {
		this.plan = plan;
	}
}